package com.ck.company.controller.client;

import com.ck.annotation.PermissionRequired;
import com.ck.company.service.CpProgramService;
import com.ck.constant.ProgramConstant;
import com.ck.constant.ResponseConstant;
import com.ck.exception.ServiceException;
import com.ck.model.common.sys.JsonResponse;
import com.ck.model.common.sys.LoginUser;
import com.ck.model.company.dto.CpProgramDTO;
import com.ck.util.SessionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 商家-节目(编辑节目客户端)
 *
 * @author linqiang
 * @date 2018-09-19 18:19
 */
@PermissionRequired
@Controller
@RequestMapping(value = "/client/program")
public class CpProgramClientController {
    private Logger logger = LoggerFactory.getLogger(CpProgramClientController.class);
    @Autowired
    private CpProgramService cpProgramService;

    /**
     * 根据节目ID获取节目
     *
     * @param programId 节目ID
     * @param request   请求
     * @return 页面
     */
    @ResponseBody
    @RequestMapping(value = "/get/{programId}", method = RequestMethod.POST)
    public Object getById(@PathVariable("programId") Long programId, HttpServletRequest request) {
        if (null == programId) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_PROGRAM_ID_NOT_NULL);
        }
        try {
            CpProgramDTO cpProgramDTO = cpProgramService.selectByPrimaryKeyDTO(programId, request);
            if (null == cpProgramDTO) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_PROGRAM_NOT_EXITS);
            }
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, cpProgramDTO, ProgramConstant.MSG_QUERY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpProgramClientController getById 根据节目ID获取节目失败: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_QUERY_FAIL);
    }

    /**
     * 编辑节目
     *
     * @param request 请求
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object updateProgram(Long programId, Long programTemplateId, String json, HttpServletRequest request) {
        if (null == programId) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_PROGRAM_ID_NOT_NULL);
        }
        if (StringUtils.isBlank(json)) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_PROGRAM_JSON_NOT_NULL);
        }
        try {
            LoginUser loginUser = SessionUtils.getLoginUserInfo(request);
            String result = cpProgramService.updateProgram(programId, programTemplateId, json, loginUser);
            if (StringUtils.isBlank(result)) {
                return new JsonResponse(ResponseConstant.CODE_SUCCESS, ProgramConstant.MSG_UPDATE_PROGRAM_SUCCESS);
            } else {
                return new JsonResponse(ResponseConstant.CODE_FAIL, result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpProgramClientController updateProgram 编辑节目失败: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_UPDATE_PROGRAM_FAIL);
    }


    /**
     * 编辑节目(保存并跳转到投放设置,需要验证节目是否符合要求)
     *
     * @param programId         节目ID
     * @param programTemplateId 模板ID
     * @param json              节目内容json
     * @param request           请求
     * @return Object
     */
    @RequestMapping(value = "/update/next", method = RequestMethod.POST)
    @ResponseBody
    public Object updateProgramAndJump(Long programId, Long programTemplateId, String json, HttpServletRequest request) {
        if (null == programId) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_PROGRAM_ID_NOT_NULL);
        }
        if (StringUtils.isBlank(json)) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_PROGRAM_JSON_NOT_NULL);
        }
        try {
            LoginUser loginUser = SessionUtils.getLoginUserInfo(request);
            String result = cpProgramService.updateProgramAndJump(programId, programTemplateId, json, loginUser);
            if (StringUtils.isBlank(result)) {
                return new JsonResponse(ResponseConstant.CODE_SUCCESS, ProgramConstant.MSG_UPDATE_PROGRAM_SUCCESS);
            } else {
                return new JsonResponse(ResponseConstant.CODE_FAIL, result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpProgramClientController updateProgram 编辑节目失败: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_UPDATE_PROGRAM_FAIL);
    }

    /**
     * 发布节目(将状态设置为‘待审批’)
     *
     * @param programId 节目主键
     * @return JsonResponse
     */
    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    @ResponseBody
    public Object publish(Long programId, HttpServletRequest request) {
        if (null == programId) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_PROGRAM_ID_NOT_NULL);
        }
        try {
            int effectRow = cpProgramService.updateProgramStatus(
                    programId, ProgramConstant.PROGRAM_STATUS_APPROVAL_PENDING, System.currentTimeMillis(), request);
            if (effectRow == 1) {
                return new JsonResponse(ResponseConstant.CODE_SUCCESS, ProgramConstant.MSG_PUBLISH_SUCCESS);
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            logger.error("CpProgramClientController publish fail: " + e.getMessage());
            return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpProgramClientController publish fail: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_PUBLISH_FAIL);
    }

    /**
     * 通过节目(将状态设置为‘已审批’)
     *
     * @param programId 节目主键
     * @param request   请求
     * @return JsonResponse
     */
    @RequestMapping(value = "/pass", method = RequestMethod.POST)
    @ResponseBody
    public Object pass(Long programId, HttpServletRequest request) {
        if (null == programId) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_PROGRAM_ID_NOT_NULL);
        }
        try {
            int effectRow = cpProgramService.approve(programId, request);
            if (effectRow == 1) {
                return new JsonResponse(ResponseConstant.CODE_SUCCESS, ProgramConstant.MSG_APPROVE_SUCCESS);
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            logger.error("CpProgramClientController pass fail: " + e.getMessage());
            return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpProgramClientController pass fail: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_APPROVE_FAIL);
    }

    /**
     * 不通过节目(将状态设置为‘待发布’)
     *
     * @param programId 节目主键
     * @param request   请求
     * @return JsonResponse
     */
    @RequestMapping(value = "/reject", method = RequestMethod.POST)
    @ResponseBody
    public Object reject(Long programId, String reason, HttpServletRequest request) {
        if (null == programId) {
            return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_PROGRAM_ID_NOT_NULL);
        }
        try {
            int effectRow = cpProgramService.notApprove(programId, System.currentTimeMillis(), reason, request);
            if (effectRow == 1) {
                return new JsonResponse(ResponseConstant.CODE_SUCCESS, ProgramConstant.MSG_NOT_APPROVE_SUCCESS);
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            logger.error("CpProgramClientController reject fail: " + e.getMessage());
            return new JsonResponse(ResponseConstant.CODE_FAIL, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpProgramClientController reject fail: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, ProgramConstant.MSG_NOT_APPROVE_FAIL);
    }
}
